package kxml_demo;

//KXML Apis
import org.kxml2.io.*;
import org.xmlpull.v1.*;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

import java.io.*;
import java.util.Vector;

import obj.LichHoc;


public class XMLJ2MEService extends MIDlet implements CommandListener {

    //Form Name
    Form mainForm = new Form ("Read Calendar from XML");
    
    //Location of xml file
    static final String URL = "http://localhost:51486/Public/Calendar/08b1.xml";
    Vector calendarVector = new Vector();
    
    StringItem resultItem = new StringItem ("", "");
    
    private final static Command xmlCommand = new Command("Get XML Data", Command.SCREEN,2);// Command.OK,1);     
    
    
    class ReadXML extends Thread {

		public void run() {
		try {
		        //Open http connection
		        HttpConnection httpConnection = (HttpConnection) Connector.open(URL);
	
			//Initilialize XML parser
			KXmlParser parser = new KXmlParser();
	
			parser.setInput(new InputStreamReader(httpConnection.openInputStream()));
			
			parser.nextTag();
				
			parser.require(XmlPullParser.START_TAG, null, "FITHOU-EDU-Calendar");
	
			//Iterate through our XML file
			while (parser.nextTag () != XmlPullParser.END_TAG)
		        	 readXMLData(parser);
	
			parser.require(XmlPullParser.END_TAG, null, "FITHOU-EDU-Calendar");
			parser.next();
	
			parser.require(XmlPullParser.END_DOCUMENT, null, null);
			

		}
		 catch (Exception e) {
	    	  	e.printStackTrace ();
	    		resultItem.setLabel ("Error:");
	    		resultItem.setText (e.toString ());

		}
	    }
     }
    
    
    public XMLJ2MEService () {
       	mainForm.append (resultItem);
    	mainForm.addCommand (xmlCommand);
	mainForm.setCommandListener (this);

	
     }
    
      
    public void startApp () {
	Display.getDisplay (this).setCurrent (mainForm);
	new ReadXML().start();
    }

    public void pauseApp () {
    }

    public void destroyApp (boolean unconditional) {
    }  

   public void commandAction(Command c, Displayable d) {
     
     StringBuffer sb = new  StringBuffer(); 	
     
     if (c == xmlCommand) {
     	    
     	    //Display parsed  XML file
     	    for(int i= 0 ; i< calendarVector.size() ;i++){
     	    	LichHoc book = (LichHoc) calendarVector.elementAt(i);
     	    	sb.append("\n");
     	    	sb.append("Ngay hoc: ");
     	    	sb.append(book.getsNgayhoc());
     	    	sb.append("\n");
     	    	sb.append("Gio bat dau: ");
     	    	sb.append(book.getsGiobatdau());
     	    	sb.append("\n");
     	    	sb.append("Gio ket thuc: ");
     	    	sb.append(book.getsGioketthuc());
     	    	sb.append("\n");
                sb.append("Trang thai: ");
     	    	sb.append(book.getsTrangthai());
     	    	sb.append("\n");
                sb.append("Giang vien: ");
     	    	sb.append(book.getsTengiangvien());
     	    	sb.append("\n");
                sb.append("Phong hoc: ");
     	    	sb.append(book.getsTenphong());
     	    	sb.append("\n");
                sb.append("Mon hoc: ");
     	    	sb.append(book.getsTenmonhoc());
     	    	sb.append("\n");
     	      }	
     	      resultItem.setLabel("Calendar: ");
     	      resultItem.setText(sb.toString());
       }
 	
    }	
      
    private void readXMLData(KXmlParser parser)
			throws IOException, XmlPullParserException {

			
			//Parse our XML file
			parser.require(XmlPullParser.START_TAG, null, "Calendar");

			LichHoc lich = new LichHoc();

			while (parser.nextTag() != XmlPullParser.END_TAG) {

				
				parser.require(XmlPullParser.START_TAG, null, null);
				String name = parser.getName();

				String text = parser.nextText();

//				System.out.println ("<"+name+">"+text);

				if (name.equals("Ngayhoc"))
					lich.setsNgayhoc(text);
				else if (name.equals("Giobatdau"))
					lich.setsGiobatdau(text);
				else if (name.equals("Gioketthuc"))
					lich.setsGioketthuc(text);
                                else if (name.equals("Trangthai"))
					lich.setsTrangthai(text);
                                else if (name.equals("Tenlop"))
					lich.setsTenlop(text);
                                else if (name.equals("GiangVien"))
					lich.setsTengiangvien(text);
                                else if (name.equals("Phonghoc"))
					lich.setsTenphong(text);
				else if (name.equals("Monhoc"))
					lich.setsTenmonhoc(text);
											
			parser.require(XmlPullParser.END_TAG, null, name);
			}
			
			calendarVector.addElement(lich);
			
			parser.require(XmlPullParser.END_TAG, null, "Calendar");
			
			
	
		}
	}



